﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentDizzy : Component, IUpdateable
    {
        public float m_BlackoutDuration;

        public float m_BlackoutFactor;

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemTime m_subsystemTime;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public ComponentPlayer m_componentPlayer;

        public float Dizzy_first;

        public float Dizzy_firstJ;

        public float Dizzy_firstT;

        public float Dizzy_firstR;

        public float Dizzy_firstS;

        public float m_DizzyRate;

        public float m_DizzyDuration;

        public Random m_random = new Random();

        public float DizzyRate => m_DizzyRate;

        public bool IsActive => m_DizzyDuration > 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void StartDizzyBuff(float time, float rate)
        {
            m_DizzyRate = rate;
            m_DizzyDuration = time;
        }

        public void StopBuff()
        {
            if (m_componentPlayer != null)
            {
                m_DizzyDuration = 0f;
                m_componentPlayer.ComponentGui.DisplaySmallMessage("眩晕结束！", Color.White, blinking: false, playNotificationSound: false);
                if ((double)Dizzy_firstR > 3.7 || Dizzy_firstR < 3f)
                {
                    Dizzy_firstR = 3.9f;
                }

                if (Dizzy_firstJ > 4.7f || Dizzy_firstJ < 3f)
                {
                    Dizzy_firstR = 4.5f;
                }

                m_componentPlayer.ComponentLocomotion.WalkSpeed = Dizzy_firstR;
                m_componentPlayer.ComponentLocomotion.FlySpeed = Dizzy_first;
                m_componentPlayer.ComponentLocomotion.JumpSpeed = Dizzy_firstJ;
                m_componentPlayer.ComponentLocomotion.TurnSpeed = Dizzy_firstT;
                m_componentPlayer.ComponentLocomotion.SwimSpeed = Dizzy_firstS;
                m_DizzyRate = 0f;
                Dizzy_first = 0f;
                Dizzy_firstR = 0f;
                Dizzy_firstJ = 0f;
                Dizzy_firstT = 0f;
                Dizzy_firstS = 0f;
            }
        }

        public void Update(float dt)
        {
            if (m_DizzyDuration > 0f && DizzyRate > 0f)
            {
                if (Dizzy_firstR == 0f)
                {
                    Dizzy_firstR = m_componentPlayer.ComponentLocomotion.WalkSpeed;
                    Dizzy_first = m_componentPlayer.ComponentLocomotion.FlySpeed;
                    Dizzy_firstJ = m_componentPlayer.ComponentLocomotion.JumpSpeed;
                    Dizzy_firstT = m_componentPlayer.ComponentLocomotion.TurnSpeed;
                    Dizzy_firstS = m_componentPlayer.ComponentLocomotion.SwimSpeed;
                }

                m_componentPlayer.ComponentLocomotion.WalkSpeed = 0f;
                m_componentPlayer.ComponentLocomotion.TurnSpeed = 0f;
                m_componentPlayer.ComponentLocomotion.FlySpeed = 0f;
                m_componentPlayer.ComponentLocomotion.SwimSpeed = 0f;
                m_componentPlayer.ComponentLocomotion.JumpSpeed = 0f;
                m_DizzyDuration -= dt;
                if (m_DizzyDuration <= 0f)
                {
                    StopBuff();
                }
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            Dizzy_first = valuesDictionary.GetValue("DizzyFirst", 0f);
            m_DizzyDuration = valuesDictionary.GetValue("DizzyDuration", 0f);
            m_DizzyRate = valuesDictionary.GetValue("DizzyRate", 0f);
            Dizzy_firstR = valuesDictionary.GetValue("DizzyR", 0f);
            Dizzy_firstJ = valuesDictionary.GetValue("DizzyJ", 0f);
            Dizzy_firstT = valuesDictionary.GetValue("DizzyT", 0f);
            Dizzy_firstS = valuesDictionary.GetValue("DizzyS", 0f);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("DizzyFirst", Dizzy_first);
            valuesDictionary.SetValue("DizzyDuration", m_DizzyDuration);
            valuesDictionary.SetValue("DizzyRate", m_DizzyRate);
            valuesDictionary.SetValue("DizzyR", Dizzy_firstR);
            valuesDictionary.SetValue("DizzyJ", Dizzy_firstJ);
            valuesDictionary.SetValue("DizzyT", Dizzy_firstT);
            valuesDictionary.SetValue("DizzyS", Dizzy_firstS);
        }
    }
}